package chixing.day08.work;

public abstract class BankAccount {
    private String accountNumber;
    private double balance;
    private static int totalAccounts = 0;// 1. 只允许 BankAccount 本类自己管理 ， 修改值，复制，获得值
                                         // 2. 对于外部 可以获得值，不能修改 set ， 不能提供set方法

    // 构造方法
    public BankAccount() {
        totalAccounts++;
    }
    public BankAccount(String accountNumber, double initialBalance) {
        this.accountNumber = accountNumber;
        this.balance = initialBalance;
        totalAccounts++;
    }

    // 存款方法
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("存入 " + amount + " 元，当前余额: " + balance + " 元");
        } else {
            System.out.println("存款金额必须为正数");
        }
    }

    // 取款方法
    public void withdraw(double amount) {
        if (amount > 0) {
            if (amount <= balance) {
                balance -= amount;
                System.out.println("取出 " + amount + " 元，当前余额: " + balance + " 元");
            } else {
                System.out.println("余额不足，无法取款");
            }
        } else {
            System.out.println("取款金额必须为正数");
        }
    }

    // 抽象方法：每月更新
    public abstract void monthlyUpdate();

    // 获取账户信息
    public String getAccountInfo() {
        return "账户号: " + accountNumber + ", 余额: " + balance + " 元";
    }

    // 获取总账户数
    public static int getTotalAccounts() {
        return totalAccounts;
    }

    // 保护方法，用于子类修改余额
    protected void setBalance(double balance) {
        this.balance = balance;
    }

    // 保护方法，用于子类获取余额
    protected double getBalance() {
        return balance;
    }

    @Override
    public String toString() {
        return "BankAccount{" +
                "accountNumber='" + accountNumber + '\'' +
                ", balance=" + balance +
                '}';
    }
}

